<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm" @submit.native.prevent>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
                class="link_a"
              >{{ form.dealName }} </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
                class="link_a"
              >{{ form.companyName }}</router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" placeholder="请输入标题" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="deal_process" :business-id="form.dealId" :btn-show="!otherBtnHide" :hidden-type-flag="true" />
    </el-form>
  </div>
</template>

<script>
    import { saveDealWfGeneral, getDealWfGeneral, getByWfNodeId } from '@/api/deal/form/dealWfGeneral'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                  actTitle: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true,
                // 是否隐藏保存按钮
                otherBtnHide: false,
                nodeName: ''
            }
        },
        created() {
            this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
              this.nodeName = data.nodeName
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealWfGeneral(data.id).then(response => {
                      this.form = response.data
                      this.loading = false
                    })
                } else {
                  getByWfNodeId(data.wfNodeId).then(response => {
                    this.form = response.data
                    this.form.fileList = data.fileList
                    this.form.actTitle = data.actTitle
                    this.loading = false
                  })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealId: this.$route.query.id,
                companyId: undefined,
                actTitle: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            /** 提交按钮 */
            submitForm: function() {
              return new Promise((resolve, reject) => {
                this.$refs['form'].validate(valid => {
                  // 验证数据
                  if (valid) {
                    // 弹出遮罩层
                    this.loading = true

                    // 保存数据
                    this.form.fileList.forEach(item => {
                      if (this.nodeName === '立项申请' || this.nodeName === '立项决议') {
                        item.directoryId = 'e03f2781f99b42028b9a353a246826fb'
                      } else if (this.nodeName === '尽调底稿') {
                        item.directoryId = 'dea5f4d35c5b4c848d83ca0fcca313fd'
                      } else if (this.nodeName === '终审申请' || this.nodeName === '终审决议') {
                        item.directoryId = '59d5b55228f0432f8a53cfe0358608a6'
                      } else if (this.nodeName === '投决会申请' || this.nodeName === '投决会决议') {
                        item.directoryId = '7cac171c21ee49c29f020214d6863356'
                      } else if (this.nodeName === '投后管理计划' || this.nodeName === '工商变更' || this.nodeName === '项目交割') {
                        item.directoryId = 'b3de2e8137214f0ba2b2409ba1ed61e2'
                      }
                    })
                    saveDealWfGeneral(this.form).then(response => {
                      if (response.code === 200) {
                        this.msgSuccess(this.$t('保存成功'))
                        this.loading = false
                        resolve(response.data.id)
                      } else {
                        this.msgError(response.msg)
                        this.loading = false
                        reject(response)
                      }
                    }).catch(() => {
                      this.loading = false
                    })
                  } else {
                    reject()
                    this.locationError(document)
                  }
                })
              })
           }

        }

    }
</script>
